extern crate fejpsl_contract;
use fejpsl_contract::exam::exam_paper::{Paper,PaperListData};
use self::fejpsl_contract::exam::exam_paper::PaperPageData;

pub async fn get_paper_list_by_page_no(page_no:&i32) -> PaperPageData {
    let client =super::super::db::connect().await.unwrap();
    let mut vec_paper = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("SELECT c.id,c.name,c.subject_id,c.score,c.question_count,c.suggest_time,
        c.limit_start_time,c.limit_end_time,c.first_category,c.second_category,c.create_time::character varying,c.collect,c.top,c.zan,
        d.nice_name,d.real_name,d.is_explicit from (select a.id,a.name,a.subject_id,a.score,a.question_count,a.suggest_time,
        limit_start_time,limit_end_time,b.level_name first_category,b.name second_category,
		a.create_time,a.create_user,a.collect,a.top,a.zan FROM public.t_exam_paper a
        left join t_subject b on a.subject_id=b.id and a.deleted=false) c left join t_user d
		on c.create_user=d.id  order by create_time desc limit $1 offset $2",
                             &[&per_page_num,&offset]).await.unwrap();
    for row in rows {
        let mut m_collect=0;
        let tmp_str: Option<i32> = row.get(11);
        if let Some(_i) = tmp_str {m_collect= row.get(11); }else{ m_collect=0}
        let mut m_top=0;
        let tmp_str: Option<i32> = row.get(12);
        if let Some(_i) = tmp_str {m_top= row.get(12); }else{ m_top=0}
        let mut m_zan=0;
        let tmp_str: Option<i32> = row.get(13);
        if let Some(_i) = tmp_str {m_zan= row.get(13); }else{ m_zan=0}
        let paper = Paper {
            id:                 row.get(0),
            name:          row.get(1),
            subject_id:    row.get(2),
            score:        row.get(3),
            question_count:            row.get(4),
            suggest_time:        row.get(5),
            first_category:        row.get(8),
            second_category:        row.get(9),
            create_time:        row.get(10),
            collect:        m_collect,
            top: m_top,
             zan: m_zan,
             nice_name: row.get(14),
             real_name: row.get(15),
             is_explicit: row.get(16),
        };
        vec_paper.push(paper);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let paper_list_data= PaperListData {
        cur_page:*page_no,
        datas:Some(vec_paper),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let paper_page_data= PaperPageData {
        data:paper_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return paper_page_data;
}
pub async fn get_paper_list_by_category(page_no:&i32,category:&String) -> PaperPageData {
    let client = super::super::db::connect().await.unwrap();
    let mut vec_paper = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;

    let rows = &client.query("SELECT c.id,c.name,c.subject_id,c.score,c.question_count,c.suggest_time,
        c.limit_start_time,c.limit_end_time,c.first_category,c.second_category,c.create_time::character varying,c.collect,c.top,c.zan,
        d.nice_name,d.real_name,d.is_explicit  from (SELECT a.id,a.name,a.subject_id,a.score,a.question_count,a.suggest_time,
        limit_start_time,limit_end_time,b.level_name first_category,b.name second_category,a.create_time,a.create_user,a.collect,a.top,a.zan
        FROM public.t_exam_paper a left join t_subject b on a.subject_id=b.id and a.deleted=false ) c left join t_user d
        on c.create_user=d.id and (first_category=$3 or second_category=$3) order by create_time desc limit $1 offset $2",
                             &[&per_page_num,&offset,&category]).await.unwrap();
    for row in rows {
        let mut m_collect=0;
        let tmp_str: Option<i32> = row.get(11);
        if let Some(_i) = tmp_str {m_collect= row.get(11); }else{ m_collect=0}
        let mut m_top=0;
        let tmp_str: Option<i32> = row.get(12);
        if let Some(_i) = tmp_str {m_top= row.get(12); }else{ m_top=0}
        let mut m_zan=0;
        let tmp_str: Option<i32> = row.get(13);
        if let Some(_i) = tmp_str {m_zan= row.get(13); }else{ m_zan=0}
        let paper = Paper {
            id:                 row.get(0),
            name:          row.get(1),
            subject_id:    row.get(2),
            score:        row.get(3),
            question_count:            row.get(4),
            suggest_time:        row.get(5),
            first_category:        row.get(8),
            second_category:        row.get(9),
            create_time:        row.get(10),
            collect:        m_collect,
            top: m_top,
            zan: m_zan,
            nice_name: row.get(14),
            real_name: row.get(15),
            is_explicit: row.get(16),

        };
        vec_paper.push(paper);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let paper_list_data= PaperListData {
        cur_page:*page_no,
        datas:Some(vec_paper),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let paper_page_data= PaperPageData {
        data:paper_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return paper_page_data;
}

pub async fn get_paper_text_content_by_subject_id(subject_id:&i32) -> String {
    let client =super::super::db::connect().await.unwrap();
    let rows = &client.query("SELECT content FROM (select * from t_exam_paper where subject_id=$1) a
            left join t_text_content b on a.frame_text_content_id=b.id ",
                             &[&subject_id]).await.unwrap();
    //let total=rows.len() as i32;
    let text_content:&str=rows[0].get(0);

    return text_content.to_string();
}
